home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / stdwin.zoo / test / dklok.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-17  |  2.5 KB  |  150 lines

  1. /* Digital clock */
  2.  
  3. #ifdef ns32000
  4. #define void int
  5. #endif
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <time.h>
  10.  
  11. #include "stdwin.h"
  12.  
  13. #include "sevenseg.h"
  14.  
  15. extern long time();
  16.  
  17. int text_only;                /* Non-zero on text-only device */
  18.  
  19. WINDOW *win;                /* Our window */
  20.  
  21. #define NIMAGE    5            /* Number of chars in image */
  22. char image[NIMAGE+1];            /* Text to draw, plus trailing \0 */
  23.  
  24. int aleft, atop, aright, abottom;    /* Rect in which to draw */
  25.  
  26. getinfo()
  27. {
  28.     wgetwinsize(win, &aright, &abottom);
  29.     wsetdocsize(win, aright, abottom);
  30.     newtime();
  31. }
  32.  
  33. newtime()
  34. {
  35.     unsigned long now;
  36.     struct tm *tp;
  37.     
  38.     wchange(win, aleft, atop, aright, abottom);
  39.     time(&now);
  40.     tp= localtime(&now);
  41.     wsettimer(win, 10 * (60 - now%60));
  42.     sprintf(image, "%02d:%02d", tp->tm_hour, tp->tm_min);
  43.     
  44. }
  45.  
  46. void
  47. drawdigit(digit, left, top, right, bottom, dh, dv)
  48.     int digit;
  49.     int left, top, right, bottom;
  50.     int dh, dv; /* Segment thickness */
  51. {
  52.     int bits= sevenseg[digit];
  53.     int mid= (top + bottom) / 2;
  54.     void (*func)();
  55.     
  56.     if (text_only)
  57.         func= winvert;
  58.     else
  59.         func= wpaint;
  60.     
  61.     if (bits & (1<<6))
  62.         func(left, top, right, top+dv);
  63.     if (bits & (1<<5))
  64.         func(left, top, left+dh, mid);
  65.     if (bits & (1<<4))
  66.         func(right-dh, top, right, mid);
  67.     if (bits & (1<<3))
  68.         func(left, mid-dv/2, right, mid+dv-dv/2);
  69.     if (bits & (1<<2))
  70.         func(left, mid, left+dh, bottom);
  71.     if (bits & (1<<1))
  72.         func(right-dh, mid, right, bottom);
  73.     if (bits & (1<<0))
  74.         func(left, bottom-dv, right, bottom);
  75. }
  76.  
  77. void
  78. drawproc(win, left, top, right, bottom)
  79.     WINDOW *win;
  80.     int left, top, right, bottom;
  81. {
  82.     int width= aright - aleft;
  83.     int height= abottom - atop;
  84.     int dh= width / (NIMAGE*6 + 1);
  85.     int dv= height / 9;
  86.     int spacing;
  87.     int i;
  88.     
  89.     if (dh < 1)
  90.         dh= 1;
  91.     if (dv < 1)
  92.         dv= 1;
  93.     
  94.     spacing= dh*6;
  95.     
  96.     for (i= 0; i < NIMAGE && image[i] != '\0'; ++i) {
  97.         if (isdigit(image[i])) {
  98.             drawdigit(image[i] - '0',
  99.                 aleft + dh + i*spacing, atop + dv,
  100.                 aleft + dh + (i+1)*spacing - dh, abottom - dv,
  101.                 dh, dv);
  102.         }
  103.     }
  104. }
  105.  
  106. main(argc, argv)
  107.     int argc;
  108.     char **argv;
  109. {
  110.     winitnew(&argc, &argv);
  111.     setup();
  112.     mainloop();
  113.     wdone();
  114.     exit(0);
  115. }
  116.  
  117. setup()
  118. {
  119.     if (wlineheight() == 1) {
  120.         text_only= 1;
  121.         wmessage("Text-only version");
  122.     }
  123.     win= wopen("DIGITAL CLOCK", drawproc);
  124.     getinfo();
  125. }
  126.  
  127. mainloop()
  128. {
  129.     for (;;) {
  130.         EVENT e;
  131.         wgetevent(&e);
  132.         switch (e.type) {
  133.         case WE_COMMAND:
  134.             switch (e.u.command) {
  135.             case WC_CLOSE:
  136.             case WC_CANCEL:
  137.                 wclose(win);
  138.                 return 0;
  139.             }
  140.             break;
  141.         case WE_SIZE:
  142.             getinfo();
  143.             break;
  144.         case WE_TIMER:
  145.             newtime();
  146.             break;
  147.         }
  148.     }
  149. }
  150.